/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package folders2;

import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
import sorting_searching.SData;

/**
 *
 * @author Administrator
 */
public class ProjectFile implements ProjectFileInterface
{
    private File myFile;
    /**
     * Constructor to initialize new projectFile
     * @param name file name
     */
    public ProjectFile(String name)
	{
		// TODO Auto-generated constructor stub
		myFile = new File(name);
	}
    /**
     * Constructor to initialize new projectFile
     * @param parent parent of the project file
     * @param name project file name
     */
	public ProjectFile(File parent,String name)
	{
		// TODO Auto-generated constructor stub
		myFile = new File(parent, name);
	}
    /**
     * initialize new projctFile with given reference
     * @param f reference to project file
     */
	public ProjectFile(File f)
	{
		// TODO Auto-generated constructor stub
		myFile = f;
	}
    /**
     * This method is used to get file
     * @return returns result of type File which is reference to this project file
     */
	public File getMyFile()
	{
		return myFile;
	}
    /**
     * This method is used to set file
     * @param myFile new file
     */
	public void setMyFile(File myFile)
	{
		this.myFile = myFile;
	}
    /**
     * This method is used to delete project file(directory && files)
     * if directory then delete every hting in it
     * if it is file only then file directly deleted
     */
	public void deleteFile()
	{
		if(this.getMyFile().exists())
		{
			if(this.getMyFile().isDirectory())
			{
				File[] children = this.getMyFile().listFiles();
				for(int i=0;i<children.length;i++)
				{
					ProjectFile child = new ProjectFile(children[i]);

					child.deleteFile();
				}
				            /*System.out.println(this.getMyFile().getName());*/
				this.getMyFile().delete();
			}
			else
			{
				this.getMyFile().delete();
			}
		}

	}
    /**
     * This method is used to copy folder contents to projectFile folder
     * @param source source folder which contains all contents
     * @throws java.io.IOException
     */
	public void copy(File source)throws IOException
	{
		if(source.isDirectory())
		{
			if(!myFile.exists())
			{
				myFile.mkdir();
			}
			String[]children = source.list();
			for(int i=0;i<children.length;i++)
			{
				ProjectFile u = new ProjectFile(myFile, children[i]);
				u.copy(new File(source,children[i]));
			}
		}
		else
		{
			InputStream in = new FileInputStream(source);
			OutputStream out = new FileOutputStream(this.getMyFile());
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0)
			{
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
		}
	}

    /*
     /**
     * This method is used to search in project file
     * @param dir
     * @param key
     * @return
    public ProjectFile search(File dir, int key) {
        File[] children = myFile.listFiles();
        if (children.length > 0) {
            SData s = new SData(children, dir);
            s.setCompareKey(0);
            s.sortList();
            File f = (File) s.binSearch();
            if (f == null) {
                return null;

            } else {
                return new ProjectFile(f);

            }
        } else {
            return null;

        }
    }
    */
    /**
     * This method is used to search for any file or directories in this project file
     * @param dir directory name
     * @return result of type ProjectFile
     * returns null if there is no file with this name
     */
    public ProjectFile search(String dir)
    {
        JFileChooser chooser = new JFileChooser();
        FileSystemView view = chooser.getFileSystemView();
        File found = view.getChild(myFile, dir);
        if(found.exists())
            return new ProjectFile(found);
        else
            return null;
    }
}
